package com.mdc.mansion.api.web.modules.basedata;


import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.core.common.restaurant.RestaurantCommon;
import com.mdc.mansion.core.modules.restaurant.RestaurantConfigService;
import com.mdc.mansion.core.modules.restaurant.RestaurantExtService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantConfig;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantExt;

@Controller
@RequestMapping(value = "${adminPath}/restaurant/config")
public class RestaurantConfigController extends BaseController {
    @Autowired
    private RestaurantConfigService restaurantConfigService;
    
    @Autowired
    private RestaurantExtService restaurantExtService;
    
    @RequestMapping("/save")
    public Result create(String data){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        JSONObject obj = JSONObject.parseObject(data);
        String type = obj.getString("configType");
        String val = obj.getString("config");
        RestaurantConfig restaurantConfig = new RestaurantConfig();
        restaurantConfig.setType(type);
        restaurantConfig.setVal(val);
        restaurantConfigService.saveOrUpdate(restaurantConfig);
        return result;
    } 
    @RequestMapping("/baseInfo")
    public Result baseInfo(){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        Map<String,String> infoMap = restaurantConfigService.baseInfo();
        result.setProperty("receiptPrintStyle", infoMap.get("receipt_print_style"));
        result.setProperty("dishesCategoryPrint", infoMap.get("dishes_category_print"));
        result.setProperty("systemConfig", infoMap.get("system_config"));
        result.setProperty("restaurantDishesUnit", JSONObject.parseArray(infoMap.get("restaurant_dishes_unit")));
        result.setProperty("imgList", JSONObject.parseArray(infoMap.get("img_list")));
        return result;
    }
    
    @RequestMapping("/deleteImg")
    public Result deleteImg(String data ){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        RestaurantExt restaurantExt = new RestaurantExt();
        JSONObject obj = JSONObject.parseObject(data);
        if(StringUtils.isBlank(obj.getString("resourceId")))
        {
        	return result;
        }
        restaurantExt.setId(obj.getString("resourceId"));
        restaurantExt.setRestaurantId(ContextHolder.currentRestaurant().getId());
        restaurantExt.setType(RestaurantCommon.Restaurant_Ext_Type_Img);
        restaurantExtService.delete(restaurantExt);
        return result;
    }
    
}
